package yxy.game.pm2.command.union.war;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionWarMessage;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.war.UnionWarGroup;
import yxy.game.pm2.module.union.war.UnionWarModule;
import yxy.game.pm2.module.union.war.UnionWarUnionModule;

/** 联盟战 联盟战对阵列表 */
@MessageType(proto = Command.UnionWarVersusList, classtype = UnionWarMessage.UnionWarVersusListRequest.class)
public class CMD_UnionWarVersusList extends Command<UnionWarMessage.UnionWarVersusListRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionWarMessage.UnionWarVersusListRequest request) {
		String playerCode = messageSource.getTag();

		UnionWarMessage.UnionWarVersusListResponse.Builder response = UnionWarMessage.UnionWarVersusListResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		{
			Result result = new UnionHandle().checkPlayerUnion(entry);
			if (!result.equals(Result.UnionIn)) {
				send(messageSource, response, result);
				return;
			}
		}
		UnionBean union = entry.getUnionBean();
		ServerBean serverBean = ServerBean.get(union.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
		AreaGroupBean areaGroupBean = AreaGroupBean.get(areaBean.getGroupId());

		UnionWarModule unionWarModule = UnionWarModule.get(areaGroupBean.getGroupId());
		for (UnionWarGroup group : unionWarModule.getGroups()) {
			UnionWarMessage.UnionWarVersus.Builder vbuilder = UnionWarMessage.UnionWarVersus.newBuilder();
			{
				UnionWarUnionModule unionA = UnionWarUnionModule.get(group.getUnionA());

				UnionWarMessage.UnionWarInfo.Builder builder = UnionWarMessage.UnionWarInfo.newBuilder();
				builder.setName(unionA.getName());
				builder.setLevel(unionA.getLevel());
				builder.setPower(unionA.getPower());
				builder.setStar(unionA.star());
				vbuilder.setUnionA(builder);
			}
			{
				UnionWarUnionModule unionB = UnionWarUnionModule.get(group.getUnionB());

				UnionWarMessage.UnionWarInfo.Builder builder = UnionWarMessage.UnionWarInfo.newBuilder();
				builder.setName(unionB.getName());
				builder.setLevel(unionB.getLevel());
				builder.setPower(unionB.getPower());
				builder.setStar(unionB.star());
				vbuilder.setUnionB(builder);
			}
			if (group.getUnionA().equals(union.getCode()) || group.getUnionB().equals(union.getCode())) {
				response.setSelfVersus(vbuilder);
			} else {
				response.addVersus(vbuilder);
			}
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionWarMessage.UnionWarVersusListResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}